/*
 *
 * CameraActivity.java
 * 
 * Created by Wuwang on 2016/11/14
 * Copyright © 2016年 深圳哎吖科技. All rights reserved.
 */
package com.wk.camerapreviewwithfilter;

import android.Manifest;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.wk.camerapreviewwithfilter.camera.CameraView;
import com.wk.camerapreviewwithfilter.filter.Filter;
import com.wk.camerapreviewwithfilter.utils.PermissionUtils;


public class CameraActivity extends AppCompatActivity {

    private CameraView mCameraView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        PermissionUtils.askPermission(this, new String[]{Manifest.permission.CAMERA, Manifest
                .permission.WRITE_EXTERNAL_STORAGE}, 10, initViewRunnable);
    }

    private Runnable initViewRunnable = new Runnable() {
        @Override
        public void run() {
            setContentView(R.layout.activity_main);
            mCameraView = findViewById(R.id.camera_view);
        }
    };

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionUtils.onRequestPermissionsResult(requestCode == 10, grantResults, initViewRunnable,
                new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(CameraActivity.this, "没有获得必要的权限", Toast.LENGTH_SHORT).show();
                        finish();
                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mCameraView != null) {
            mCameraView.onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mCameraView != null) {
            mCameraView.onPause();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_filter, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        String name = item.getTitle().toString();
        if (name.equals("切换摄像头")) {
            mCameraView.switchCamera();
        }
        switch (item.getItemId()) {
            case R.id.mDeal:
                mCameraView.switchCamera();
                break;
            case R.id.mDefault:
                mCameraView.setFilter(Filter.NONE);
                break;
            case R.id.mGray:
                mCameraView.setFilter(Filter.GRAY);
                break;
            case R.id.mCool:
                mCameraView.setFilter(Filter.COOL);
                break;
            case R.id.mWarm:
                mCameraView.setFilter(Filter.WARM);
                break;
            case R.id.mBlur:
                mCameraView.setFilter(Filter.BLUR);
                break;
//            case R.id.mMagn:
//                mCameraView.setFilter(Filter.MAGN);
//                break;

        }
        return super.onOptionsItemSelected(item);
    }


}
